

// Our working URL. This is required to let the AJAX requests work right!
//var workURL = 'http://www.artife.net:8080/chatbox/';
var workURL = 'http://localhost:8081/chatbox/';
//var workURL = 'http://localhost:80/chatbox/';

// Our main variable where the main chatbox class is stored. We place it here
// so everywhere in the script we can use it without using very complex
// design patterns for passing the main instance along.
var chat;

// The libraries import queue. (actually, it's not a queue.. it's a library array
// where we can store which libraries are already imported).
var cimport_queue = new Array();

// Whether the loading screen is LOCKED in place. You can't hide it, but you can change it!
var loadLock = false;

// Show a loading screen
// string msg: The message you want the use to show. When nothing specified, see type variable what it will be
// int type: Specifies what kind of loading screen you want.
// Type 1: Full screen. When no message given the message will be 'Loading...'
// Type 2: Synchronizing div. When no message given the message will empty.
ShowLoading = function(msg, type)
{
	// If no type is specified, we assume we want to use the normal full screen version.
	if (type == null)
		type = 1;
	
	// Full screen or just the synchronizing div?
	if (type == 2)
	{
		// No message; empty message.
		if (msg == null)
			msg = '';
			
		// Add the ajax-loading image.
		msg = msg + "<img src=\"" + workURL + "ui/ajax-loader.gif\">";
		
		// Set the message
		$('synchronizing').innerHTML = msg;
		
	} // end if
	else
	{
		// No message; just say 'Loading...', is clear enough to the user
		if (msg == null)
			msg = 'Loading...';
		
		// Add the all important CSS class to it. 
		$('loading').innerHTML = "<div class=\"loading_\">" + msg + "</div>";
		
		// Display the loading and the curtain. The curtain prevents the user from doing something else.
		$('loading').style.display = 'block';
		$('curtain').style.display = 'block';
		
	} // end else
	
}; // end function ShowLoading

// Hide the loading screen IF the loading screen is not locked ofcourse.
HideLoading = function()
{
	// Lock? cancel!
	if (loadLock == true)
		return;
	
	// Hide the 'full screen' loading.
	$('loading').style.display = 'none';
	$('curtain').style.display = 'none';
	
	// Set the synchronizing element (main window of the chatbox) back to empty
	$('synchronizing').innerHTML = '';
	
}; // end function HideLoading

// This function is a 'fix' for the String.Replace() which I found didn't work too well
// It only works for single char replacements for now.
replaceAllSingleChar = function(s, t, r)
{
	var tmp = "";
	var c = "";
	for (var i = 0; i <= s.length; i++)
	{
		c = s.substring(i, i+1);
		
		if (c == t)
			tmp += r;
		else
			tmp += c;
			
	}
	
	return tmp;
	
};

// Load a Javascript library.
// You can also add an onLoad event on it, and pass a variable with it.
cimport = function(name, action, atr)
{
	// Is this library already loaded? Don't load it again!
	if (cimport_queue[name] != null)
	{
		// Set the status to a special status where we can execute the onLoad event again.
		cimport_queue[name]['status'] = 2;
		
		// Specify new event if it's different
		if (action != cimport_queue[name]['act'])
			cimport_queue[name]['act'] = action;
		
		if (atr != cimport_queue[name]['att'])
			cimport_queue[name]['att'] = atr;
		
		// Execute the event
		cimport_loaded(name);
		
		// Bye.
		return;
		
	} // end if
    
    // When no event is given, specify nothing for attributes
	if (action == null)
	{
		action = null;
		atr = null;
		
	} // end if
	
	// Save the library events and stuff.
    cimport_queue[name] = new Array();
    cimport_queue[name]['status'] = 1;
    cimport_queue[name]['act'] = action;
    cimport_queue[name]['att'] = atr;
	
	// Give the user a message that we're loading something.
	ShowLoading('Loading libraries (' + name + ')');
	
	var head = document.getElementsByTagName('head').item(0);
	
    var script = document.createElement('script');
    
    script.setAttribute('language', 'javascript');
    script.setAttribute('type', 'text/javascript');
    
    script.setAttribute('src',  workURL + "ui/js/" + replaceAllSingleChar(name, ".", "/") + ".js");
    
    head.appendChild(script);
    
} // end function cimport

// Function fired when the library is loaded.
// This function fires the event that has been hung on the onLoad thing..
cimport_loaded = function(name)
{
	// hide the loading message
	HideLoading();
	
	// Does this library actually exist?
	if (cimport_queue[name] != null)
	{
		// Get the info
		var arr = cimport_queue[name];
		
		// The event has already been fired without any new request of loading it.
		if (arr['status'] < 1)
			return;
		
		// If there is an event.. fire it
		if (arr['act'] != null)
			arr['act'](arr['att']);
		
		// Reset the status to zero.
		cimport_queue[name]['status'] = 0;
		
	} // end if
	
} // end function cimport_loaded

// This function enables hovering for the tag you want.
// Mozilla has standard support for it. Although Internet Explorer only likes to
// allow the :hover CSS class on anchor elements, this function triggers the
// ...._hover CSS class when a hover on the element occures. 
enable_hovering_for = function(tag)
{
	$$(tag).forEach(function(el)
	{
		el.onmouseover = function()
		{
			this.className = this.className + '_hover';
			$('debug').innerHTML = 'changed className to: ' + this.className;
		} // end event function onmouseover
		el.onmouseout = function()
		{
			this.className = this.className.substring(0, this.className.length-6);
			$('debug').innerHTML = 'changed className to: ' + this.className;
		} // end event function onmouseout
		
	}); // end foreach
	
} // end function

// This function removes the onmouseover (The hover effects) from the tags you want.
// The oppposite of enable_hovering_for ;)
disable_hovering_for = function(tag)
{
	$$(tag).forEach(function(el)
	{
		el.onmouseover = function() { return; } // end event function onmouseover
		el.onmouseout = function() { return; } // end event function onmouseout
		
	}); // end foreach
	
} // end function

// The boot function. This creates a new instance in the chat variable
// and starts our application!
var boot = function()
{
	chat = new Chatbox(workURL);
	chat.start();
}

// Boot sequence. It imports some libraries required for our application to work
// The sequence is started at the event window.onload
boot_load1 = function() { cimport('core.Session', boot_load2, null); 		}
boot_load2 = function() { cimport('core.Dialogs', boot_load3, null); 		}
boot_load3 = function() { cimport('core.AjaxRequest', boot_load4, null); 	}
boot_load4 = function() { cimport('core.Form', boot_load5, null); 			}
boot_load5 = function() { cimport('panels.base', boot_load6, null); 		}
boot_load6 = function() { cimport('core.Chatbox', boot, null); 			}
window.onload = function() { cimport('md5', boot_load1, null); 			}

// Some function requird to enable drag... :')
function $S(){
	var els = [];
	$A(arguments).each(function(sel){
		if ($type(sel) == 'string')
			els.extend(document.getElementsBySelector(sel));
		else if ($type(sel) == 'element') 
			els.push($(sel));
	});
	return $$(els);
}; // end function $S
